import Vue from 'vue';
import Router from 'vue-router';

// import memberDetails from '@/components/member/memberDetails';
// import information from '@/components/member/information/information';
import menu from '@/components/menu';
import home from '@/components/menu/home';
import report from '@/components/menu/report';
import my from '@/components/menu/my';
import login from '@/components/menu/login';
import registers from '@/components/menu/registers';
import _import from './_import_production';

Vue.use(Router);

const routers = new Router({
  mode: 'history',//隐藏路由中的 #
  routes: [
    {//主菜单
      path: '/',
      name: 'menu',
      component: menu,
      children:[
        {//首页
          path: '/home',
          name: 'home',
          component: home,
          meta: {
            title: '首页'
          }
        },
        {//统计
          path: '/report',
          name: 'report',
          component: report,
          meta: {
            title: '统计'
          }
        },
        {//我的
          path: '/my',
          name: 'my',
          component: my,
          meta: {
            title: '我的'
          }
        }
      ]
    },
    {//登录
      path: '/login',
      name: 'login',
      component: login,
      meta: {
        title: '登录'
      }
    },
    {//注册
      path: '/registers',
      name: 'registers',
      component: registers,
      meta: {
        title: '注册'
      }
    },
    {
      path: '/member',
      name: 'member',
      component: _import('member'),
      meta: {
        title: '会员查询'
      }
    },
    {
      path: '/memberDetails',
      name: 'memberDetails',
      component: _import('member/memberDetails'),
      meta: {
        title: '会员详情'
      }
    },
    {
      path: '/integration',
      name: 'integration',
      component: _import('member/integration'),
      meta: {
        title: '积分记录'
      }
    },
    {
      path: '/balance',
      name: 'balance',
      component: _import('member/balance'),
      meta: {
        title: '余额记录'
      }
    },
    {
      path: '/recharge',
      name: 'recharge',
      component: _import('member/recharge'),
      meta: {
        title: '充值记录'
      }
    },
    {
      path: '/growth',
      name: 'growth',
      component: _import('member/growth'),
      meta: {
        title: '成长值记录'
      }
    },
    {
      path: '/reappearance',
      name: 'reappearance',
      component: _import('member/reappearance'),
      meta: {
        title: '积分返现记录'
      }
    },
    {//做测试页面
      path: '/demo',
      name: 'demo',
      component: _import('demo'),
      children: [  //这里就是二级路由的配置
        {
          path: '/demoScroll',
          name: 'demoScroll',
          component:  _import('demo/demoScroll')
        }
        ]
    },
    {//会员生日
      path: '/birthday',
      name: 'birthday',
      component: _import('birthday'),
      meta: {
        title: '会员生日提醒'
      }
    },
    {//库存
      path: '/stock',
      name: 'stock',
      component: _import('stock'),
      meta: {
        title: '库存'
      }
    },
    {//经销商列表
      path: '/customer',
      name: 'customer',
      component: _import('customer'),
      meta: {
        title: '经销商'
      }
    },
    {//经销商详情
      path: '/customerInfo',
      name: 'customerInfo',
      component: _import('customer/customerInfo'),
      meta: {
        title: '经销商详情'
      }
    },//customerAccount
    {//经销商账户
      path: '/customerAccount',
      name: 'customerAccount',
      component: _import('customerAccount'),
      meta: {
        title: '经销商账户'
      }
    },
    {//经销商账户详情
      path: '/customerAccountInfo',
      name: 'customerAccountInfo',
      component: _import('customerAccount/customerAccountInfo'),
      meta: {
        title: '经销商账户详情'
      }
    },
    {//会员消费分析
      path: '/memberConsumption',
      name: 'memberConsumption',
      component: _import('memberConsumption'),
      meta: {
        title: '会员消费分析'
      }
    },
    {//会员分布分析
      path: '/memberDistribution',
      name: 'memberDistribution',
      component: _import('memberDistribution'),
      meta: {
        title: '会员分布分析'
      }
    },
    {//会员新增同比
      path: '/memberAdded',
      name: 'memberAdded',
      component: _import('memberAdded'),
      meta: {
        title: '会员新增同比'
      }
    },
    {//店仓销售排行榜
      path: '/storeSalesRankings',
      name: 'storeSalesRankings',
      component: _import('storeSalesRankings'),
      meta: {
        title: '店仓销售排行榜'
      }
    },
    {//店员销售排行榜
      path: '/clerkSalesRankings',
      name: 'clerkSalesRankings',
      component: _import('clerkSalesRankings'),
      meta: {
        title: '店员销售排行榜'
      }
    },
    {//商品进销汇总
      path: '/goodsImportSales',
      name: 'goodsImportSales',
      component: _import('goodsImportSales'),
      meta: {
        title: '商品进销汇总'
      }
    },
    {//商品销售排行
      path: '/goodsSaleRankings',
      name: 'goodsSaleRankings',
      component: _import('goodsSaleRankings'),
      meta: {
        title: '商品销售排行'
      }
    },
    {//终端零售商品排行
      path: '/retailTerminalGoodsRankings',
      name: 'retailTerminalGoodsRankings',
      component: _import('retailTerminalGoodsRankings'),
      meta: {
        title: '终端零售商品排行'
      }
    },
    {//终端零售排行
      path: '/retailTerminalRankings',
      name: 'retailTerminalRankings',
      component: _import('retailTerminalRankings'),
      meta: {
        title: '终端零售排行'
      }
    },
    {//跨级统计
      path: '/crossLevelSupervise',
      name: 'crossLevelSupervise',
      component: _import('crossLevelSupervise'),
      meta: {
        title: '跨级统计'
      }
    },
    {//经销商时销统计
      path: '/dealerStatistics',
      name: 'dealerStatistics',
      component: _import('dealerStatistics'),
      meta: {
        title: '经销商统计'
      }
    },//dealerStatisticsInfo
    {//终端时销详情
      path: '/dealerStatisticsInfo',
      name: 'dealerStatisticsInfo',
      component: _import('dealerStatistics/dealerStatisticsInfo'),
      meta: {
        title: '经销商统计'
      }
    },
    {//考勤打卡
      path: '/attend',
      name: 'attend',
      component: _import('attend'),
      meta: {
        title: '考勤打卡'
      }
    },
    {//考勤记录
      path: '/attend/record',
      name: 'record',
      component: _import('attend/record'),
      meta: {
        title: '考勤记录'
      }
    }
  ]
});


//导航守卫（路由跳转前验证登录）
// to: Route: 即将要进入的目标 路由对象
// from: Route: 当前导航正要离开的路由
// next: Function: 一定要调用该方法来 resolve 这个钩子。执行效果依赖 next 方法的调用参数。
routers.beforeEach((to, from, next) => {
  // console.log(localStorage.getItem("type"))
  // 代理商路由
  const router = ['menu', 'attend', 'record', 'home', 'report', 'my', 'member','memberDetails','integration','recharge','balance','growth','reappearance','birthday','stock','memberConsumption','memberDistribution','memberAdded','storeSalesRankings','clerkSalesRankings','goodsImportSales','goodsSaleRankings'];

  // 经销商路由
  const nextRoute = ['menu', 'attend', 'record', 'home', 'report', 'my','member','memberDetails','integration','recharge','balance','growth','reappearance','birthday','stock','customer','customerInfo','customerAccount','customerAccountInfo','memberConsumption','memberDistribution','memberAdded','storeSalesRankings','clerkSalesRankings','goodsImportSales','goodsSaleRankings','retailTerminalGoodsRankings','retailTerminalRankings','crossLevelSupervise','dealerStatistics','dealerStatisticsInfo','demo'];


  let isLogin = parseInt(localStorage.getItem("isLogin"));  // 是否登录


  // 判断是否为代理商 0-代理商 1-经销商
  if (localStorage.getItem("type")==0) {
    // console.log('代理商')
    // 未登录状态；当路由到nextRoute指定页时，跳转至login
    if (router.indexOf(to.name) >= 0) {
      if (!isLogin) {
        next({name: 'login'});
      }else{
        next();
      }
    }

  }else{
    // console.log('经销商')
    // 未登录状态；当路由到nextRoute指定页时，跳转至login
    if (nextRoute.indexOf(to.name) >= 0) {
      if (!isLogin) {
        next({name: 'login'});
      }else{
        next();
      }
    }

  }

  // 已登录状态；当路由到login时，跳转至home
  if (to.name == 'login') {
    if (isLogin) {
      next({ name: 'home' });
    }else{
      next();
    }
  }

  if(to.name == 'registers'){
    // console.log('注册')
    // next({ name: 'registers' });
  }


});

export default routers;

